/**
 * @author Federico Consoli
 *
 * Released under GNU GPLv3 (http://www.gnu.org/licenses/gpl-3.0.txt)
 */

#include "libreria.h"
#include "Service_1.h"

// Cast for the accept function
#define CAST_ADDR(x) ( struct sockaddr *)(x)

#define cs(x) Connection::char_to_string(x)
#define cus(x) Connection::char_to_uns_short(x)

Service_1 *Ser1=NULL;
Server *registro_locale=NULL;


/**
 * This function chose the local services for the client
 * @param sd socket descriptor of the client
 * @param R pointer to the Responce
 */
void Server::scegli(int sd, Response *R) {

    //Confronto tra il servizio da avviare e quello ricevuto
    //Il client invia un oggetto di tipo services e vengono controllati TUTTI i parametri
    if (!R->confronto(sd)) {
        cout << "ERRORE: Richiesta Servizio incoerente!" << endl;
        return;
    }

    string Nome = R->testa_elenco_servizi->nome_servizio;
    if (Nome == "Rotate image") Ser1->rotate(R, sd);
    else if (Nome == "Horizontal Flip image") Ser1->mirror(R, sd);
    else cout << display() + "Service not found\n";

}

int main(int argc, const char * argv[]) {
    if ((argc ==2 && argv[1][1]=='h')) {
        cout << "Sintassi: ./server1 <ip_reg> <port_reg> <base_ip> <base_port> <name>\n\n";
        cout << "Parameters are preparatory in order of entry, so you can entry no value\n";
        cout << "Or you can entry only ip_reg or only ip_reg+port_reg etc. etc.\n\n";
        cout << "ip_reg default: 127.0.0.1\n";
        cout << "port_reg default: 40000\n";
        cout << "base_ip default: 127.0.0.1\n";
        cout << "base_port default: 30000 (Attention here, see doc for info about base_port!) \n";
        cout << "name default: 'SERVER 1' \n\n";
        return 0;
    }

  try {
         
        
        switch (argc) {
            case 1:
                registro_locale= new Server;
                Ser1= new Service_1;
                break;
            case 2:
                registro_locale= new Server(cs(argv[1])); //string
                Ser1= new Service_1;
                break;
            case 3:
                registro_locale= new Server(cs(argv[1]),cus(argv[2])); //string. u s
                Ser1= new Service_1;
                break;
            case 4:
                registro_locale= new Server(cs(argv[1]),cus(argv[2])); //string. u s
                Ser1= new Service_1(cs(argv[3])); //string
                break;
            case 5:
                registro_locale= new Server(cs(argv[1]),cus(argv[2])); //string. u s
                Ser1= new Service_1(cs(argv[3]),cus(argv[4])); //string us
                break;
            case 6:
                registro_locale= new Server(cs(argv[1]),cus(argv[2])); //string. u s
                Ser1= new Service_1(cs(argv[3]),cus(argv[4]),cs(argv[5])); //string us string
                break;
                
        }
        if (Ser1!=NULL && registro_locale!=NULL)
        Ser1->menu(registro_locale);
        
    }
    catch (int num_exc) {
        Exception_class::exc(num_exc);
    }
    delete Ser1;
    delete registro_locale;
    return 0;
}



